package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.dto.result.ApiResponse;
import com.example.domain.dto.result.RepairResult;
import com.example.domain.entity.Repair;
import com.example.domain.query.RepairQuery;
import com.example.factory.ResultFactory;
import com.example.service.IRepairService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 设备报修表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2025-05-15
 */
@CrossOrigin
@RestController
@RequestMapping("/repair")
@Tag(name = "设备报修管理器")
public class RepairController {

    @Autowired
    private IRepairService repairService;

    @Autowired
    private ResultFactory resultFactory;

    /**
     * 分页查询设备报修信息
     * @param query 查询条件
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询设备报修信息")
    public IPage<RepairResult> pageQuery(@RequestBody RepairQuery query,
                                         @RequestParam(defaultValue = "1") int pageNum,
                                         @RequestParam(defaultValue = "10") int pageSize) {
        return resultFactory.convertRepairPage(repairService.pageQuery(query, pageNum, pageSize));
    }

    /**
     * 添加设备报修信息
     * @param repair 报修信息
     * @return 操作结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加设备报修信息")
    public ApiResponse addRepair(@RequestBody Repair repair) {
        boolean result = repairService.addRepair(repair);
        return result ? ApiResponse.success("添加成功") : ApiResponse.error("添加失败");
    }

    /**
     * 更新设备报修信息
     * @param repair 报修信息
     * @return 操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新设备报修信息")
    public ApiResponse updateRepair(@RequestBody Repair repair) {
        boolean result = repairService.updateRepair(repair);
        return result ? ApiResponse.success("更新成功") : ApiResponse.error("更新失败");
    }

    /**
     * 删除设备报修信息
     * @param repairId 报修ID
     * @return 操作结果
     */
    @DeleteMapping("/delete/{repairId}")
    @Operation(summary = "删除设备报修信息")
    public ApiResponse deleteRepair(@PathVariable Integer repairId) {
        boolean result = repairService.deleteRepair(repairId);
        return result ? ApiResponse.success("删除成功") : ApiResponse.error("删除失败");
    }

    /**
     * 根据ID获取设备报修信息
     * @param repairId 报修ID
     * @return 报修信息
     */
    @GetMapping("/get/{repairId}")
    @Operation(summary = "根据ID获取设备报修信息")
    public ApiResponse getRepairById(@PathVariable Integer repairId) {
        Repair repair = repairService.getById(repairId);
        return repair != null ? ApiResponse.success(repair) : ApiResponse.error("未找到相关报修信息");
    }

    @GetMapping("/scrapAudit")
    @Operation(summary = "报废审核")
    public ApiResponse scrapAudit(Integer repairId, Integer audit, String auditComment, String repairPlan) {
        return this.repairService.scrapAudit(repairId, audit, auditComment, repairPlan);
    }
}
